package ks.server.processor;

import ks.framework.common.Message;
import ks.framework.communicator.Communicator;
import ks.framework.debug.Debug;
import ks.server.controllers.commands.admin.ReportController;
import ks.server.controllers.commands.admin.icon.IconAddController;
import ks.server.controllers.commands.admin.icon.IconDeleteController;
import ks.server.controllers.commands.admin.plugin.PluginClearController;
import ks.server.controllers.commands.admin.plugin.PluginInstallController;
import ks.server.controllers.commands.admin.plugin.PluginSuspendController;
import ks.server.controllers.commands.admin.plugin.PluginUninstallController;
import ks.server.controllers.commands.admin.plugin.PluginUnsuspendController;
import ks.server.controllers.commands.admin.user.UserBlockController;
import ks.server.controllers.commands.admin.user.UserCreateController;
import ks.server.controllers.commands.admin.user.UserDeleteController;
import ks.server.controllers.commands.admin.user.UserKickController;
import ks.server.controllers.commands.admin.user.UserListController;
import ks.server.controllers.commands.admin.user.UserUnblockController;
import ks.server.controllers.commands.general.ChatController;
import ks.server.controllers.commands.general.IconListController;
import ks.server.controllers.commands.general.LoginController;
import ks.server.controllers.commands.general.LogoutController;
import ks.server.controllers.commands.general.game.FinishController;
import ks.server.controllers.commands.general.plugin.PluginInfoController;
import ks.server.controllers.commands.general.plugin.PluginListController;
import ks.server.controllers.commands.general.table.AcceptController;
import ks.server.controllers.commands.general.table.JoinController;
import ks.server.controllers.commands.general.table.LeaveController;
import ks.server.controllers.commands.general.table.OptionsController;
import ks.server.controllers.commands.general.table.RejectController;
import ks.server.controllers.commands.general.table.TableChatController;
import ks.server.controllers.commands.general.table.TablesController;
import ks.server.controllers.commands.general.user.AccountController;
import ks.server.controllers.commands.general.user.UserConnectedController;
import ks.server.controllers.commands.general.user.UserInfoController;

/**
 * Server-side processor of messages received from client.
 * <p>
 * You will need to replace this code with something more meaningful. I would
 * suggest a chain-of-responsibility pattern, as we will discuss in class.
 * 
 * @author George Heineman
 * @author Harry Moreno
 * @author Christopher Page
 */
public class Processor  {
	
	/** Agent who knows how to communicate back to client(s). */
	Communicator com;
	
	public Processor (Communicator com) {
		this.com = com;
	}
	
	public boolean process(Message m) {

		Debug.println("Received:" + m.toString());
		
		// general-command: chat
		if (m.getName().equalsIgnoreCase("chat"))
			return new ChatController(com).process(m);

		// general-command: icon-list
		if (m.getName().equalsIgnoreCase("icon-list"))
			return new IconListController(com).process(m);

		// general-command: login
		if (m.getName().equalsIgnoreCase("login"))
			return new LoginController(com).process(m);

		// general-command: logout
		if (m.getName().equalsIgnoreCase("logout"))
			return new LogoutController(com).process(m);

		// general-command: plugin-info
		if (m.getName().equalsIgnoreCase("plugin-info"))
			return new PluginInfoController(com).process(m);

		// general-command: plugin-list
		if (m.getName().equalsIgnoreCase("plugin-list"))
			return new PluginListController(com).process(m);

		// general-command: table: accept
		if (m.getName().equalsIgnoreCase("accept"))
			return new AcceptController(com).process(m);

		// general-command: table: finish
		if (m.getName().equalsIgnoreCase("finish"))
			return new FinishController(com).process(m);

		// general-command: table: join
		if (m.getName().equalsIgnoreCase("join"))
			return new JoinController(com).process(m);

		// general-command: table: leave
		if (m.getName().equalsIgnoreCase("leave"))
			return new LeaveController(com).process(m);

		// general-command: table: options
		if (m.getName().equalsIgnoreCase("options"))
			return new OptionsController(com).process(m);

		// general-command: table: reject
		if (m.getName().equalsIgnoreCase("reject"))
			return new RejectController(com).process(m);
		
		//TODO implement the following controller
		// general-command: table: start
		if (m.getName().equalsIgnoreCase("start"))
			return false;

		// general-command: table: table-chat
		if (m.getName().equalsIgnoreCase("table-chat"))
			return new TableChatController(com).process(m);

		// general-command: table: tables
		if (m.getName().equalsIgnoreCase("tables"))
			return new TablesController(com).process(m);
		
		//TODO implement the following controller
		// general-command: table: update
		if (m.getName().equalsIgnoreCase("update"))
			return false;

		// general-command: user: account
		if (m.getName().equalsIgnoreCase("account"))
			return new AccountController(com).process(m);

		// general-command: user: user-connected
		if (m.getName().equalsIgnoreCase("user-connected"))
			return new UserConnectedController(com).process(m);

		// general-command: user: user-info
		if (m.getName().equalsIgnoreCase("user-info"))
			return new UserInfoController(com).process(m);
		
		// admin-command: report
		if (m.getName().equalsIgnoreCase("report"))
			return new ReportController(com).process(m);
		
		// admin-command: icon-add
		if (m.getName().equalsIgnoreCase("icon-add"))
			return new IconAddController(com).process(m);
		
		// admin-command: icon-delete
		if (m.getName().equalsIgnoreCase("icon-delete"))
			return new IconDeleteController(com).process(m);

		// admin-command: plugin-clear
		if (m.getName().equalsIgnoreCase("plugin-clear"))
			return new PluginClearController(com).process(m);

		// admin-command: plugin-install
		if (m.getName().equalsIgnoreCase("plugin-install"))
			return new PluginInstallController(com).process(m);

		// admin-command: plugin-suspend
		if (m.getName().equalsIgnoreCase("plugin-suspend"))
			return new PluginSuspendController(com).process(m);

		// admin-command: plugin-uninstall
		if (m.getName().equalsIgnoreCase("plugin-uninstall"))
			return new PluginUninstallController(com).process(m);

		// admin-command: plugin-unsuspend
		if (m.getName().equalsIgnoreCase("plugin-unsuspend"))
			return new PluginUnsuspendController(com).process(m);

		// admin-command: user-block
		if (m.getName().equalsIgnoreCase("user-block"))
			return new UserBlockController(com).process(m);

		// admin-command: user-create
		if (m.getName().equalsIgnoreCase("user-create"))
			return new UserCreateController(com).process(m);

		// admin-command: user-delete
		if (m.getName().equalsIgnoreCase("user-delete"))
			return new UserDeleteController(com).process(m);

		// admin-command: user-kick
		if (m.getName().equalsIgnoreCase("user-kick"))
			return new UserKickController(com).process(m);

		// admin-command: user-list
		if (m.getName().equalsIgnoreCase("user-list"))
			return new UserListController(com).process(m);

		// admin-command: user-unblock
		if (m.getName().equalsIgnoreCase("user-unblock"))
			return new UserUnblockController(com).process(m);
		
		// return false if we have no idea how to process this message.
		return false;
	}
}
